<?php

class Workset_Controller_Plugin_ActionTracer extends Zend_Controller_Plugin_Abstract {

    protected $_requests = array();

    protected $_traceList = array();

    protected $_traceParams = array();

    protected $_useRegistry = false;

    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        $this->_addRequest($request);
    }

    public function dispatchLoopShutdown() {
        //dump($this->_requests);
    }

    public function useRegistry($flag) {
        $this->_useRegistry = (bool)$flag;
    }

    public function traceAction($action, $controller = null, $module = null, $actionParams = null) {

        $request = Zend_Controller_Front::getInstance()->getRequest();

        if ($action instanceof Zend_Controller_Action) {
            $obj = $action;
            $action = $obj->getRequest()->getActionName();
            $controller = $obj->getRequest()->getControllerName();
            $module = $obj->getRequest()->getModuleName();
            $params = $obj->getRequest()->getParams();
        } else {
            $controller = $controller ? (string)$controller : $request->getControllerName();
            $module = $module ? (string)$module : $request->getModuleName();
            $params = $request->getParams();
        }

        if (null !== $actionParams) {

            $actionParams = (array)$actionParams;
            $values = array_values($actionParams);
            $keys = array_keys($actionParams);

            $params = array_intersect_key($params, array_flip($values));

            foreach ($keys as $i => $key) {
                if (is_string($key)) {
                    $realKey = $values[$i];

                    $params[$key] = $params[$realKey];
                    unset($params[$realKey]);
                }
            }
        }

        $key = count($this->_traceList);
        
        $this->_traceList[$key] = array($action, $controller, $module);
        $this->_traceParams[$key] = $params;

        return $this;
    }

    protected function _addRequest(Zend_Controller_Request_Abstract $request) {

        if (!empty($this->_traceList)) {
            $data = array(
                $request->getActionName(),
                $request->getControllerName(),
                $request->getModuleName(),
            );

            if (in_array($data, $this->_traceList)) {
                $req = clone $request;

                if ($this->_useRegistry) {
                    if (!Zend_Registry::isRegistered('requestList')) {
                       Zend_Registry::set('requestList', array());
                    }

                    $stack = Zend_Registry::get('requestList');
                    $stack[] = $req;

                    Zend_Registry::set('requestList', $stack);
                }
                
                $this->_requests[] = $req;
            }

            return;
        }

        $this->_requests[] = clone $request;
    }

}